package com.nobody.facepk.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.nobody.facepk.service.ServiceRegistry;
import com.nobody.facepk.tools.ImageTool;
import com.nobody.facepk.utils.Constants;
import com.nobody.facepk.utils.UserUtils;

@Controller
public class UploadController {
	
	@RequestMapping("/upload.do")
	public String upload(HttpServletRequest request) {
		MultipartHttpServletRequest multipartRequest  =  (MultipartHttpServletRequest) request;
		MultipartFile imgFile  =  multipartRequest.getFile("imgFile");
//		String name = imgFile.getName();
//		String originName = imgFile.getOriginalFilename();
//		long size = imgFile.getSize();
//		String type = imgFile.getContentType();
//		System.out.println("name:"+name+" originName:"+originName+" size:"+size+" type:"+type);
		
//		String real = request.getRealPath("/");
		if(Constants.IMAGE_STORE==null) Constants.IMAGE_STORE = request.getRealPath("/")+"pictures/";
		Long userId = UserUtils.getUserId();
		try {
			String originName = imgFile.getOriginalFilename();
			String ext = originName.substring(originName.lastIndexOf('.'));
			String path = Constants.IMAGE_STORE+userId+"/";
			File dir = new File(path);
			dir.mkdirs();
			
			String time = System.currentTimeMillis()+"";
			
			//保存原图片
			String srcName = time;
			File srcFile = new File(path+srcName);
			imgFile.transferTo(srcFile);
			
			//保存中缩略图
			String mFile = path+time+"_m.jpg";
			ImageTool.compressImage(srcFile, mFile, 400, 400, true);
			
			//保存小缩略图
			String sFile = path+time+"_s.jpg";
			ImageTool.compressImage(srcFile, sFile, 120, 120, true);
			
			ServiceRegistry.getPictureService().addPicture(userId, time);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(),e);
		}
		return "uploadOk";
	}
//	final static int IMAGE_SIZE = 120;   
//	public static void createPreviewImage(String srcFile, String destFile) {   
//        try {   
//            File fi = new File(srcFile); // src   
//            File fo = new File(destFile); // dest   
//            BufferedImage bis = ImageIO.read(fi);   
//  
//            int w = bis.getWidth();   
//            int h = bis.getHeight();   
//            double scale = (double) w / h;   
//            int nw = IMAGE_SIZE; // 
//            int nh = (nw * h) / w;   
//            if (nh > IMAGE_SIZE) {   
//                nh = IMAGE_SIZE;   
//                nw = (nh * w) / h;   
//            }   
//            double sx = (double) nw / w;   
//            double sy = (double) nh / h;   
//  
//            transform.setToScale(sx, sy);   
//            AffineTransformOp ato = new AffineTransformOp(transform, null);   
//            BufferedImage bid = new BufferedImage(nw, nh,   
//                    BufferedImage.TYPE_3BYTE_BGR);   
//            ato.filter(bis, bid);   
//            ImageIO.write(bid, " jpeg ", fo);   
//        } catch (Exception e) {   
//            e.printStackTrace();   
//            throw new RuntimeException(   
//                    " Failed in create preview image. Error:  "  
//                            + e.getMessage());   
//        }   
//    } 
}
